package com.nativetalk.service.member.search;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.FoundChange.TdNtMembFoundChangeList;
import com.nativetalk.bean.SystemConfig;
import com.nativetalk.bean.TdNtPayActivity;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.SystemConfigDao;
import com.nativetalk.dao.TdNtMembFoundChangeDao;
import com.nativetalk.dao.TdNtMemberDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/1/9.
 * Modified by wangzheng on 17/3/8
 */
@Service
public class MemberSearchServiceImpl implements  MemberSearchService{
    private static Logger logger = Logger.getLogger(MemberSearchServiceImpl.class);
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Resource
    private TdNtMembFoundChangeDao tdNtMembFoundChangeDao;

    @Resource
    private SystemConfigDao systemConfigDao;
    @Override
    public RetInfo findMemberBalance(Integer memb_id) {
        String logInfo = this.getClass().getName() + ":findMemberBalance:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        Map<String , TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String , TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
        BigDecimal study_amount = new BigDecimal(tsNtWebSiteConfIndexMap.get("25").getData_value());
        try {
            Map<String , Object> map = new HashMap<>();
            map.put("memb_balance" , tdNtMemberDao.findTdNtMemberBalabce(memb_id));
            map.put("amount", study_amount);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取余额成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public RetInfo findMemberBalanceThree(Integer memb_id) {
        String logInfo = this.getClass().getName() + ":findMemberBalanceThree:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        Map<String , TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String , TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
        BigDecimal study_amount = new BigDecimal(tsNtWebSiteConfIndexMap.get("25").getData_value());
        try {
            //查询满赠的信息
            List<TdNtPayActivity> tdNtPayActivity = (List<TdNtPayActivity>) MemcachedUtils.get("TdNtPayActivityIndex");
            try {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("memb_id",memb_id);
                map.put("currentPage",0);
                map.put("pageSize",10);

                List<TdNtMembFoundChangeList> foundChangeLists = tdNtMembFoundChangeDao.findTdNtMembFoundChangePage(map);
                if (foundChangeLists == null || foundChangeLists.size() <= 0) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip(ValidationMessages.getValue("MEMB.FOUN.RESULT.NULL"));
                } else {
                    for(TdNtMembFoundChangeList foundChangeList : foundChangeLists){
                        if(null != foundChangeList.getType() && 4 == foundChangeList.getType()){
                            foundChangeList.setMfchg_change_name("其它");
                        }
                    }
                }

                SystemConfig systemCourseVipPriceForYear = systemConfigDao.findByCode("systemCourseVipPriceForYear");

                Map<String, Object> result = new HashMap<>();
                result.put("foundChangeLists", foundChangeLists);
                result.put("payactivity" , tdNtPayActivity);
                result.put("memb_balance" , tdNtMemberDao.findTdNtMemberBalabce(memb_id));
                result.put("amount", study_amount);
                result.put("vipPrice", Double.parseDouble(systemCourseVipPriceForYear.getValue()));
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取余额成功");
            } catch (Exception e) {
                retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
                retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
                logger.error(e.getMessage(),e);
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    @Override
    public BigDecimal findMemberBalanceStudy(Integer memb_id) {
        return tdNtMemberDao.findTdNtMemberBalabce(memb_id);
    }

    @Override
    public TdNtMemberDetail findTdNtMemberDetail(Integer memb_id) {
        return tdNtMemberDao.findTdNtMemberDetail(memb_id);
    }

    public TdNtMember findById(Integer membId){
        return tdNtMemberDao.findMember(membId);
    }
}
